package com.wingsfrog.puzzle.util.wechat;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class WechatConst {
    public static String appid = "wx1af3d7165f94ed69";
    public static String secret = "cad5c8a70c4dec48d8b006664027d48e";
    public static String tokenServerUrl = "http://localhost:8000/irons/access_token.php";
    private static String oAuthUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%appid%&redirect_uri=%redirectUri%&response_type=code&scope=%scope%&state=%state%#wechat_redirect";
    private static String snsCodeUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%appid%&secret=%secret%&code=%code%&grant_type=authorization_code";
    private static String snsUserInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=%access_token%&openid=%openid%&lang=zh_CN";
    public static String scopeBase = "snsapi_base";
    public static String scopeUserInfo = "snsapi_userinfo";
    public static String redirectUri = "http://wingsfrog.com/puzzle/users/wxLogin";
    public static String travelFront = "http://wingsfrog.com/t";
    public static String jigsawGame = "";

    public static String createOauthUrl(String redirectUri) throws UnsupportedEncodingException {
        return createOauthUrl(appid, redirectUri, scopeBase);
    }

    public static String createOauthUrl(String redirectUri,
                                        String state) throws UnsupportedEncodingException {
        return createOauthUrl(appid, redirectUri, state, scopeBase);
    }

    public static String createOauthUrl(String appid,
                                        String redirectUri,
                                        String scope) throws UnsupportedEncodingException {
        return createOauthUrl(appid, redirectUri, "STATE", scope);
    }

    public static String createOauthUrl(String appid,
                                        String redirectUri,
                                        String state,
                                        String scope) throws UnsupportedEncodingException {
        return oAuthUrl.replace("%appid%", appid)
                .replace("%redirectUri%", URLEncoder.encode(redirectUri,"utf8"))
                .replace("%state%", URLEncoder.encode(state, "utf8"))
                .replace("%scope%", scope);
    }

    public static String createSnsCodeUrl(String code) {
        return snsCodeUrl.replace("%appid%", appid).replace("%secret%", secret).replace("%code%", code);
    }

    public static String createSnsUserInfoUrl(String accessToken, String openid) {
        return snsUserInfoUrl.replace("%access_token", accessToken).replace("%openid%", openid);
    }
}
